#ifndef TFFMPEGAUDIOPLAYTHREAD_H
#define TFFMPEGAUDIOPLAYTHREAD_H

#include <QThread>
#include <QAudioOutput>
#include "tffmpegsupporter.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
#include <libavutil/ffversion.h>
#include <libavutil/imgutils.h>
#include <libswresample/swresample.h>
}

class TFFmpegAudioPlayThread : public QThread
{
    Q_OBJECT
public:
    explicit TFFmpegAudioPlayThread(QObject *parent = nullptr);
    ~TFFmpegAudioPlayThread() override;

    //开始播放
    void startPlay();
    //停止播放
    void stopPlay();
    //初始化音频
    bool initAudio();
    //初始化音频设备
    void initAudioDevice(int nsamplerate,int nsamplesize,int nchncnt);
    //解码音频
    void decodeAudio(AVPacket *npkt);
    //解码音频 参考SSPlayer
    void decodeAudio2(AVPacket *npkt);
    //设置时钟
    void setStartTime(int64_t nstarttime);
    //设置时间基准
    void setTimeBase(AVRational ntimebase);

protected:
    void run() override;

private:
    volatile bool m_stopped = true;           //线程停止标志位
    qint64 m_startTime;                       //解码开始时间
    AVRational m_timeBase;                    //时间基准

    AVCodecContext *m_audioCtx = nullptr;     //音频解码器
    SwrContext *m_audioSwrCtx = nullptr;      //音频转换
    AVCodec *m_audioCodec = nullptr;          //音频解码
    AVFrame *m_audioFrame = nullptr;          //帧对象音频

    QIODevice *m_audioDevice = nullptr;       //音频播放设备
    QAudioOutput *m_audioOutput = nullptr;    //音频播放对象

    uint8_t *m_audioData = nullptr;           //解码后的音频数据

    TFFCustomPlayer &m_ffcustomPlayer = TFFmpegSupporter::get()->getFFCustomPlayer();

};

#endif // TFFMPEGAUDIOPLAYTHREAD_H
